// 本地测试环境
//var resPath = "http://localhost:8080/";

// 手机本地测试环境
//var resPath = "http://192.168.137.5:8081/bfc_website/";

// 生产测试环境
var resPath = "http://java.learn.english.yokol.top/";

var video_resPath = "http://learn.english.audio.yokol.top/";

// 格式化时间
Date.prototype.Format = function(fmt) { //author: meizz 
	var o = {
		"M+": this.getMonth() + 1, //月份 
		"d+": this.getDate(), //日 
		"h+": this.getHours(), //小时 
		"m+": this.getMinutes(), //分 
		"s+": this.getSeconds(), //秒 
		"q+": Math.floor((this.getMonth() + 3) / 3), //季度 
		"S": this.getMilliseconds() //毫秒 
	};
	if(/(y+)/.test(fmt)) fmt = fmt.replace(RegExp.$1, (this.getFullYear() + "").substr(4 - RegExp.$1.length));
	for(var k in o)
		if(new RegExp("(" + k + ")").test(fmt)) fmt = fmt.replace(RegExp.$1, (RegExp.$1.length == 1) ? (o[k]) : (("00" + o[k]).substr(("" + o[k]).length)));
	return fmt;
}
// 给日期类对象添加日期差方法，返回日期与diff参数日期的时间差，单位为天
Date.prototype.diff = function(date) {
	return(this.getTime() - date.getTime()) / (24 * 60 * 60 * 1000);
}

$.ajaxSetup({
//	contentType:"application/json",
	beforeSend: function(XMLHttpRequest) {
		// 设置请求头 
//		var token = localStorage.getItem("token");
//      XMLHttpRequest.setRequestHeader("token",token == undefined ? "none" : token);
	},
	crossDomain: true, // 设置跨域请求，此种形式需要浏览器支持
	xhrFields:{withCredentials:true} // 设置浏览器身份，下次提交会把浏览器信息一并提交后台，可以避免cookie不一致情况
});

// 将表格序列化成json数据
$.fn.serializeJson = function() {
	var serializeObj = {};
	var array = this.serializeArray();
	var str = this.serialize();
	$(array).each(
		function() {
			if(serializeObj[this.name]) {
				if($.isArray(serializeObj[this.name])) {
					serializeObj[this.name].push(this.value);
				} else {
					serializeObj[this.name] = [
						serializeObj[this.name], this.value
					];
				}
			} else {
				serializeObj[this.name] = this.value;
			}
		});
	return serializeObj;
};

// 获取url路径上的参数值
function getUrlParam(name) {
	var reg = new RegExp("(^|&)" + name + "=([^&]*)(&|$)");
	var r = window.location.search.substr(1).match(reg);
	if(r != null) return unescape(r[2]);
	return null;
}

function clearForm(id) {
    var objId = document.getElementById(id);
    if (objId == undefined) {
        return;
    }
    for (var i = 0; i < objId.elements.length; i++) {
        if (objId.elements[i].type == "text") {
            objId.elements[i].value = "";
        }
        else if (objId.elements[i].type == "password") {
            objId.elements[i].value = "";
        }
        else if (objId.elements[i].type == "radio") {
            objId.elements[i].checked = false;
        }
        else if (objId.elements[i].type == "checkbox") {
            objId.elements[i].checked = false;
        }
        else if (objId.elements[i].type == "select-one") {
            objId.elements[i].options[0].selected = true;
        }
        else if (objId.elements[i].type == "select-multiple") {
            for (var j = 0; j < objId.elements[i].options.length; j++) {
                objId.elements[i].options[j].selected = false;
            }
        }
        else if (objId.elements[i].type == "textarea") {
            objId.elements[i].value = "";
        }
        //else if (objId.elements[i].type == "file") {
        // //objId.elements[i].select();
        // //document.selection.clear();
        // // for IE, Opera, Safari, Chrome
        // var file = objId.elements[i];
        // if (file.outerHTML) {
        // file.outerHTML = file.outerHTML;
        // } else {
        // file.value = ""; // FF(包括3.5)
        // }
        //}
    }
}

function playVideo(soundSource){
	soundSource = soundSource == null || soundSource == undefined ? "http://i.xiao84.com/en-yinbiao/mp3/48yinbiaomp3/e-sound.mp3" : soundSource;
	if(soundSource == "" || soundSource == "null"){
		alert("十分抱歉，没有可以播放的单词地址");
		return;
	}
	var player = $("#hideAudio");
	if(player == null || player == undefined || player.length < 1){
		$("body").append('<audio id="hideAudio"><source id="player"></source></audio>');
	};
	player = $("#hideAudio")[0]; 
	$("#player").attr("src",video_resPath + soundSource);
	player.load();
	player.play();
}